define(['app'], function(app) {
    app.controller('AceSearchboxController', function(xContext, $uibModalInstance) {
        var self = this;

        self.init = function() {
            self.editor = xContext.editor;
            self.context = xContext.context;
            $uibModalInstance.rendered.then(function() {
                var dialog = angular.element('#ace-searchbox').parents('div.modal');
                dialog.draggable({
                    cursor: 'move',
                    handle: '.modal-header'
                });
                dialog.css('overflow', 'hidden');
                dialog.css('left', ($(window).width() - dialog.width()) / 2 + 'px');
            });
        }

        self.find = function() {
            self.editor.find(self.context.needle, self.context);
        }

        self.replace = function() {
            self.editor.find(self.context.needle, self.context);
            self.editor.replace(self.context.replace);
        }

        self.replaceAll = function() {
            self.editor.find(self.context.needle, self.context);
            self.editor.replaceAll(self.context.replace);
        }

        self.close = function() {
            $uibModalInstance.close();
        }
    });
});
